﻿using Provider;
using System.Collections.Generic;
using System.Configuration;
using System;
using System.Linq;
using MahdieLabDataBaseModel;

public partial class ExceptionManagement_ExceptionHandler : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["ExceptionHandler"] != null)
            {
                List<Provider.ExceptionHandler> ExecutionCollection = (List<Provider.ExceptionHandler>)Session["ExceptionHandler"];
                MahdieLabDataBaseModel.ExceptionHandler _ExceptionHandler = new MahdieLabDataBaseModel.ExceptionHandler();
                using (DefaultEntityConnection _DefaultEntityConnection = new DefaultEntityConnection())
                {
                    foreach (Provider.ExceptionHandler exceptionHandler in ExecutionCollection)
                    {
                        try
                        {
                            if (exceptionHandler.ExceptionType == ExceptionType.UnHandeled ||
                                exceptionHandler.ExceptionType == ExceptionType.PageUrl)
                            {
                                string InnerException = exceptionHandler.Exception.InnerException.Message.ToString();
                                //if (InnerException.Contains("\""))
                                //  InnerException = InnerException.Substring(0, InnerException.IndexOf('"'));
                                //if (_DefaultEntityConnection.ExceptionHandler.Where(c => c.InnerException == InnerException) == null)
                                //    InnerException = "Fatal Error";
                                //_ExceptionHandler = _DefaultEntityConnection.ExceptionHandler.Where(c => c.InnerException == InnerException).First();
                                ////
                                if (InnerException.Contains("\'"))
                                    InnerException = InnerException.Substring(0, InnerException.IndexOf('\''));
                                if (_DefaultEntityConnection.ExceptionHandlers.Where("it.InnerException Like '%" + InnerException + "%'").ToList().Count < 1)
                                    InnerException = "Fatal Error";
                                _ExceptionHandler = _DefaultEntityConnection.ExceptionHandlers.Where("it.InnerException Like '%" + InnerException + "%'").First();                                
                            }
                            else
                            {
                                string InnerException = exceptionHandler.ExceptionType.ToString();
                                if (_DefaultEntityConnection.ExceptionHandlers.Where(c => c.InnerException == InnerException) == null)
                                    InnerException = "Fatal Error";
                                _ExceptionHandler = _DefaultEntityConnection.ExceptionHandlers.Where(c => c.InnerException == InnerException).First();
                                _ExceptionHandler.Message = string.Format(_ExceptionHandler.Message, exceptionHandler.ParameterTitle);
                                _ExceptionHandler.HelpNote = string.Format(_ExceptionHandler.HelpNote, exceptionHandler.ParameterTitle);
                            }
                            ExceptionHandlerBulletedList.Items.Add(string.Format("{0} : {1}. {2}. \n {3}", _ExceptionHandler.Title, _ExceptionHandler.Message, _ExceptionHandler.HelpNote,_ExceptionHandler.InnerException));
                        }
                        catch
                        {
                            ExceptionHandlerBulletedList.Items.Add(string.Format("{0} : {1}. {2}. \n {3}", ConfigurationManager.AppSettings["DefaultExceptionTitle"], ConfigurationManager.AppSettings["DefaultExceptionMessage"], ConfigurationManager.AppSettings["DefaultExceptionHelpNote"], exceptionHandler.Exception.InnerException.ToString()));
                        }
                    }
                }
                Session["ExceptionHandler"] = null;
            }
        }
        catch
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "CloseForm", "window.close();", true);
        }
    }
}
